package cn.edu.lingnan.service.impl;


import cn.edu.lingnan.client.PointClient;
import cn.edu.lingnan.entity.dto.ProductDto;
import cn.edu.lingnan.entity.pojo.Product;
import cn.edu.lingnan.mapper.ProductMapper;
import cn.edu.lingnan.service.ProductService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
@Slf4j
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {

    private  final PointClient pointClient;
    @Override
    @Transactional
    public boolean saveProduct(Product product,Integer point) {
        boolean result = this.save(product);
        if (result) {
            pointClient.add(product.getProductId(),point);
            return true;
        }
        return false;
    }

    @Override
    public Page<Product> getProductsByPage(ProductDto productDto) {
        // 创建分页对象，传入当前页码和每页大小
        Page<Product> page = new Page<>(productDto.getPageNum(), productDto.getPageSize());
         log.info("当前页码：{}，每页大小：{}", productDto.getPageNum(), productDto.getPageSize());

        // 构建查询条件
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();

        // 条件：品牌ID
        if (productDto.getBrandId() != null) {
            queryWrapper.eq("brand_id", productDto.getBrandId());
        }

        // 条件：商品名称模糊查询
        if (productDto.getProductName() != null && !productDto.getProductName().isEmpty()) {
            queryWrapper.like("product_name", productDto.getProductName());
        }

        // 条件：状态
        if (productDto.getStatus() != null) {
            queryWrapper.eq("status", productDto.getStatus());
        }

        // 使用分页对象和查询条件进行查询
        return this.page(page, queryWrapper);
    }



}
